<?php
require_once ('./View/ArticleEditorView.php');
require_once ('./Model/ArticleEditorModel.php');
require_once ('./Model/Validation.php');
require_once ('./Model/DBSettings.php');
require_once ('./Static/Messages.php');
require_once ('./Static/ArrayAssoc.php');

class ArticleEditorController{
	public static $userId;
	private $validation;
	private $articleEditView;
	private $articleEditModel;
	/*
	 * Tar emot $action som inparameter och returnerar html för akrivande av ny artikel, editering av vald artikel 
	 * eller en artikellista för användaren
	 * 
	 * @return string HTML
	 */	
	public function DoControll($action){
		$db = new mysqli(DBSettings::$host, DBSettings::$user, DBSettings::$password, DBSettings::$database);
		
		$this->validation = new Validation();
		$this->articleEditView = new ArticleEditorView();
		$this->articleEditModel = new ArticleEditorModel($db);
		
		switch ($action) {
			case Action::ARTICLE_WRITE:
				return $this->ArticleSubmit();
				break;
			case Action::ARTICLE_EDIT:
				return $this->ArticleEdit();
				break;
			case Action::ARTICLE_LIST:
				return $this->ArticleList();
				break;				
			default:
				return $this->ArticleList();
				break;					
		}

	}
	//Returnerar en lista med alla artiklar för aktuell användare samt sätter eventuella felmeddelande
	//Om användaren valt att ta bort en artikel tas den bort innan ny lista skapas
	public function ArticleList(){
		if(($articleId = $this->articleEditView->GetRemoveId()) != null){
			if(!$this->articleEditModel->RemoveArticle($articleId, self::$userId)){
				ArticleEditorView::$message = Messages::ERROR_REMOVE_FAIL;
				$articleArray = $this->articleEditModel->GetArticleArray(self::$userId);
				return $this->articleEditView->DoArticleList($articleArray, true);	
			}
		}
		$articleArray = $this->articleEditModel->GetArticleArray(self::$userId);
		return $this->articleEditView->DoArticleList($articleArray);
	}
	//returnerar ett formulär för editering av vald artikel, i det fall användaren submittat en editerad artikel kallas DoSubmit
	public function ArticleEdit(){
		if(($articleId = $this->articleEditView->GetEditId()) != null){
			if($this->articleEditView->TriedToSubmit()){		
				return $this->DoSubmit($articleId);
			}
			$articleArray = $this->articleEditModel->GetArticle($articleId, self::$userId);
			return $this->articleEditView->DoArticleSubmitBox($articleArray[ArrayAssoc::ARTICLE_HEADLINE], $articleArray[ArrayAssoc::ARTICLE_DESC]);	
		}
		return $this->articleEditView->DoArticleSubmitBox();
	}
	//returnerar ett formulär för ny artikel, i det fall användaren submittat en artikel kallas DoSubmit
	public function ArticleSubmit(){
		
		if($this->articleEditView->TriedToSubmit()){		
			return $this->DoSubmit();
		}
		return $this->articleEditView->DoArticleSubmitBox();		
	}	
	//Validerar indata och sätter eventuella felmeddelande, om inga fel finns skickas artikelt till modellen för inskrivning i databas
	public function DoSubmit($articleId = null){			
		if(!$this->validation->checkHeadline($this->articleEditView->GetHeadline())){
			ArticleEditorView::$message = Messages::ERROR_HEADLINE_INVALID;
			return $this->articleEditView->DoArticleSubmitBox($this->articleEditView->GetHeadline(), $this->articleEditView->GetArticle(), true);
		}
		if(!$this->validation->checkArticle($this->articleEditView->GetArticle())){
			ArticleEditorView::$message = Messages::ERROR_ARTICLE_INVALID;
			return $this->articleEditView->DoArticleSubmitBox($this->articleEditView->GetHeadline(), $this->articleEditView->GetArticle(), true);
		}
		if($this->articleEditModel->SubmitArticle($this->articleEditView->GetHeadline(), $this->articleEditView->GetArticle(), self::$userId, (!is_null($articleId)) ? $articleId : null)){
			ArticleEditorView::$message = Messages::SUCCESS_ARTICLESUBMISSION_COMPLETE;
			return $this->articleEditView->DoArticleSubmitBox();
		}
		ArticleEditorView::$message = Messages::SUCCESS_ARTICLESUBMISSION_COMPLETE;
		return $this->articleEditView->DoArticleSubmitBox();
				
	}

}
?>